package sse.ustc.cc.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import sse.ustc.cc.activity.DayClassInfoAty;
import sse.ustc.cc.activity.DayEntryAty;
import sse.ustc.cc.activity.MonthEntryAty;
import sse.ustc.cc.activity.WeekEntryAty;

public class ScaleGestureListener implements OnScaleGestureListener {
		private float beforeFactor;
		private float mPivotX;
		private float mPivotY;
		private View mVSouce;
		private Context mContext;
		private String tag;
		
		public ScaleGestureListener(View mVSouce, Context mContext, String tag) {
			// TODO Auto-generated constructor stub
			this.mVSouce = mVSouce;
			this.mContext = mContext;
			this.tag = tag;
		}
		
		@Override
		public boolean onScale(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			if (checkIsNull()) {
				return false;
			}

			final float factor = detector.getScaleFactor();
			Animation animation = new ScaleAnimation(beforeFactor, factor,
					beforeFactor, factor, mPivotX, mPivotY);
		
			animation.setFillAfter(true);
			
			mVSouce.startAnimation(animation);		
			beforeFactor = factor;
			return true;
		}

		@Override
		public boolean onScaleBegin(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			if (checkIsNull()) {
				return false;
			}
			beforeFactor = 1f;
			mPivotX = detector.getFocusX() - mVSouce.getLeft();
			mPivotY = mVSouce.getTop() + (mVSouce.getHeight() >> 1);	
			return true;
		}

		@Override
		public void onScaleEnd(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			if (checkIsNull()) {
				return;
			}
			
			mVSouce.clearAnimation();
			if(tag.equals("day_class"))
				mContext.startActivity(new Intent(mContext, WeekEntryAty.class));
			else if(tag.equals("week_class"))
				mContext.startActivity(new Intent(mContext, MonthEntryAty.class));
			
			((Activity) mContext).finish();
		}
		
		public boolean checkIsNull() {
			return mVSouce == null ? true : false;
		}	
	}